﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ZLDC.Common.Estate;

namespace ZLDC.Estate
{
                public class Utility
    {
                                        public static string GetHousingCondition()
        {
            string condition = "";

            string usage = _Request.GetString("usage", "");                                         if (usage != "")
                condition += " and [use] in(" + usage + ")";

            /************** 楼盘栋阁过滤条件 ********************/
            string conditionBuild = "";
            int tmp_id = 0;                 int level = 0;      
            string buildName = _Request.GetString("name", "");                                      if (buildName != "")
            {
                string[] buildList = _Utility.GetStringBySplit(buildName, ',');
                conditionBuild += " and (";

                for (int i = 0; i < buildList.Length; i++)
                {
                    string item = buildList[i];

                    tmp_id = ZLDC.Topo.BLL.Estate.BuildBLL.GetBuildByName(item, ref level);

                    if (tmp_id > 0)
                    {
                        if (i == 0)
                        {
                            if (level == 0)
                                conditionBuild += "eid = " + tmp_id + "";
                            if (level == 1)
                                conditionBuild += "level1 = " + tmp_id + "";
                            if (level == 2)
                                conditionBuild += "level2 = " + tmp_id + "";
                            if (level == 3)
                                conditionBuild += "level3 = " + tmp_id + "";
                        }
                        else
                        {
                            if (level == 0)
                                conditionBuild += " or eid = " + tmp_id + "";
                            if (level == 1)
                                conditionBuild += " or level1 = " + tmp_id + "";
                            if (level == 2)
                                conditionBuild += " or level2 = " + tmp_id + "";
                            if (level == 3)
                                conditionBuild += " or level3 = " + tmp_id + "";
                        }
                    }
                }
                conditionBuild += " )";

                                            }

            condition += conditionBuild;
            /*********************************************************************/

            int room = _Request.GetInt("room", 0);                                      if (room > 0)
                condition += " and room=" + room;

            int hall = _Request.GetInt("hall", 0);                                      if (hall > 0)
                condition += " and hall=" + hall;

            int acreage1 = _Request.GetInt("acreage1", 0);                              int acreage2 = _Request.GetInt("acreage2", 0);                              if (acreage1 > 0 && acreage2 > 0)
                condition += " and acreage >=" + acreage1 + " and acreage < " + acreage2;

            int did = _Request.GetInt("did", 0);                                        if (did > 0)
                condition += " and did=" + did;

            int aid = _Request.GetInt("aid", 0);                                        if (aid > 0)
                condition += " and aid=" + aid;

            int status = _Request.GetInt("status", 0);                                  if (status > 0)
                condition += " and status=" + status;

            int tradetype = _Request.GetInt("tradetype", 0);                            if (tradetype > 0)
                condition += " and tradeType=" + tradetype;

            int price1 = _Request.GetInt("price1", 0);                                  int price2 = _Request.GetInt("price2", 0);                                  if (price1 > 0 && price2 > 0)
            {
                if (tradetype == 1)
                    condition += " and totalprice >=" + price1 + " and totalprice < " + price2;
                else
                    condition += " and leaseprice >=" + price1 + " and leaseprice < " + price2;
            }
            else if (price1 > 0)
            {
                if (tradetype == 1)
                    condition += " and totalprice >=" + price1;
                else
                    condition += " and leaseprice >=" + price1;
            }

            int hasKey = _Request.GetInt("hasKey", 0);                                  if (hasKey > 0)
                condition += " and hasKey=" + hasKey;

            int isOnly = _Request.GetInt("isOnly", 0);                                  if (isOnly > 0)
                condition += " and isOnly=" + isOnly;

            int hasPhoto = _Request.GetInt("hasPhoto", 0);                              if (hasPhoto > 0)
                condition += " and hasPhoto=" + hasPhoto;

            int pocStatus = _Request.GetInt("pocStatus", 0);                            if (pocStatus > 0)
                condition += " and pocStatus='红本'";

            int mark2 = _Request.GetInt("mark2", 0);                                    if (mark2 > 0)
                condition += " and mark2='满5年免税'";

            return condition;
        }

                                        public static string GetHousingConditionByAdv()
        {
            string condition = "";

            string usage = _Request.GetString("usage", "");                                         if (usage != "")
                condition += " and [use] in(" + usage + ")";

            /************** 楼盘栋阁过滤条件 ********************/
            string conditionBuild = "";
            int tmp_id = 0;                 int level = 2;      
            string buildIdList = _Request.GetString("buildlist", "");                               string buildIdLevel = _Request.GetString("buildlevel", "");                               if (!buildIdList.Equals("") && !buildIdList.Equals(","))
            {
                string[] buildList = null;
                string[] buildLevel = null;
                if (buildIdList != "" && buildIdList != ",")
                {
                    buildList = _Utility.GetStringBySplit(buildIdList, ',');
                    buildIdList = buildIdList.Substring(0, buildIdList.Length - 1);
                }

                if (buildIdLevel != "" && buildIdLevel != ",")
                {
                    buildLevel = _Utility.GetStringBySplit(buildIdLevel, ',');
                    buildIdLevel = buildIdLevel.Substring(0, buildIdLevel.Length - 1);
                }

                conditionBuild += " and (";
                string fk = " 1=1 ";
                for (int i = 0; i < buildList.Length; i++)
                {
                    tmp_id = _Convert.ToInt(buildList[i], 0);
                    if (buildLevel != null && buildLevel.Length > i)
                        level = _Convert.ToInt(buildLevel[i], 2);
                    
                    if (tmp_id > 0)
                    {
                        fk = "";
                        if (i == 0)
                        {
                            if (level == 0)
                                conditionBuild += "eid = " + tmp_id + "";
                            if (level == 1)
                                conditionBuild += "level1 = " + tmp_id + "";
                            if (level == 2)
                                conditionBuild += "level2 = " + tmp_id + "";
                            if (level == 3)
                                conditionBuild += "level3 = " + tmp_id + "";
                        }
                        else
                        {
                            if (level == 0)
                                conditionBuild += " or eid = " + tmp_id + "";
                            if (level == 1)
                                conditionBuild += " or level1 = " + tmp_id + "";
                            if (level == 2)
                                conditionBuild += " or level2 = " + tmp_id + "";
                            if (level == 3)
                                conditionBuild += " or level3 = " + tmp_id + "";
                        }
                    }

                                                                                                                                                            }
                conditionBuild += " " + fk + ")";

                            }
            else
            {
                string buildName = _Request.GetString("name", "");                                          if (buildName != "")
                {
                    string[] buildList = _Utility.GetStringBySplit(buildName, ',');
                    conditionBuild += " and (";
                    string fk = " 1=1 ";
                    for (int i = 0; i < buildList.Length; i++)
                    {
                        string item = buildList[i];

                        tmp_id = ZLDC.Topo.BLL.Estate.BuildBLL.GetBuildByName(item, ref level);

                        if (tmp_id > 0)
                        {
                            fk = "";
                            if (i == 0)
                            {
                                if (level == 0)
                                    conditionBuild += "eid = " + tmp_id + "";
                                if (level == 1)
                                    conditionBuild += "level1 = " + tmp_id + "";
                                if (level == 2)
                                    conditionBuild += "level2 = " + tmp_id + "";
                                if (level == 3)
                                    conditionBuild += "level3 = " + tmp_id + "";
                            }
                            else
                            {
                                if (level == 0)
                                    conditionBuild += " or eid = " + tmp_id + "";
                                if (level == 1)
                                    conditionBuild += " or level1 = " + tmp_id + "";
                                if (level == 2)
                                    conditionBuild += " or level2 = " + tmp_id + "";
                                if (level == 3)
                                    conditionBuild += " or level3 = " + tmp_id + "";
                            }
                        }

                                                                                                                                                                                            }
                    conditionBuild += " " + fk + ")";

                                                        }
            }

            condition += conditionBuild;
            /*********************************************************************/

            int room = _Request.GetInt("room", 0);  
            int room1 = _Request.GetInt("room1", 0);            if (room > 0 &&  room1==0)
                condition += " and room=" + room;

            
            if (room1 > 0 && room==0) 
            {
                condition += " and room=" + room1;
            }
            if (room > 0 && room1 > 0) 
            {
                condition += " and room >=" + room + "  and room <=" + room1 + "";
            }

            int hall = _Request.GetInt("hall", 0);                                                  if (hall > 0)
                condition += " and hall=" + hall;

            int acreage1 = _Request.GetInt("acreage1", 0);                                          int acreage2 = _Request.GetInt("acreage2", 0);                                          if (acreage1 > 0)
            {
                condition += " and acreage >=" + acreage1;
            }
            if (acreage2 > 0)
            {
                condition += " and acreage <= " + acreage2;
            }
            int did = _Request.GetInt("did", 0);                                                    if (did > 0)
                condition += " and did=" + did;

            int aid = _Request.GetInt("aid", 0);                                                    if (aid > 0)
                condition += " and aid=" + aid;

            int tradetype = _Request.GetInt("tradetype", 0);                            
            if (_Utility.GetCompanyName() == "长沙")
            {

                if (!ZLDC.Common.BaseUser.UserInfo.GetPopedom("查询租盘") || !ZLDC.Common.BaseUser.UserInfo.GetPopedom("查询售盘"))
                {
                    if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("查询租盘"))
                        condition += " and tradeType=2 ";

                    if (ZLDC.Common.BaseUser.UserInfo.GetPopedom("查询售盘"))
                        condition += " and tradeType=1 ";
                }
              
            }
            
            if (tradetype > 0)
                   condition += " and tradeType=" + tradetype;
            

            int price1 = _Request.GetInt("price1", -1);                                              int price2 = _Request.GetInt("price2", -1);                                              
            if (price1 >= 0)
            {
                if (tradetype == 1)
                    condition += " and totalprice >=" + price1;
                else if (tradetype == 2)
                    condition += " and leaseprice >=" + price1;
                else
                    condition += " and leaseprice >=" + price1 * 10000;
                
            }
            if (price2 >=0)
            {
                if (tradetype == 1)
                    condition += " and totalprice < " + price2;
                else if (tradetype == 2)
                    condition += " and leaseprice < " + price2;
                else
                    condition += " and leaseprice <" + price2 * 10000;
                
            }

                                                                                                
            int hasKey = _Request.GetInt("hasKey", 0);                                              if (hasKey > 0)
                condition += " and hasKey=" + hasKey;

            int isOnly = _Request.GetInt("isOnly", 0);                                              if (isOnly > 0)
                condition += " and isOnly=" + isOnly;

            int isCty = _Request.GetInt("isCty", 0);                                                if (isCty > 0)
                condition += " and ctyCont=" + isCty;

            int hasPhoto = _Request.GetInt("hasPhoto", 0);                                          if (hasPhoto > 0)
                condition += " and hasPhoto=" + hasPhoto;

            int pocStatus = _Request.GetInt("pocStatus", 0);                                        if (pocStatus > 0)
                condition += " and pocStatus='红本'";

            int mark2 = _Request.GetInt("mark2", 0);                                                if (mark2 > 0)
                condition += " and mark2='满5年免税'";

                        string linkman = _Request.GetString("linkman", "");                                     if (linkman != "")
                condition += " and owner='" + linkman + "'";

            string linktel = _Request.GetString("linktel", "");                                     if (linktel != "")
                condition += " and ownertel like '%" + linktel + "%'";

            int unitPrice1 = _Request.GetInt("unitPrice1", 0);                                      int unitPrice2 = _Request.GetInt("unitPrice2", 0);                                      if (unitPrice1 > 0 && unitPrice2 > 0)
                condition += " and unitPrice >=" + unitPrice1 + " and unitPrice <= " + unitPrice2;
            else if (unitPrice1 > 0)
                condition += " and unitPrice >=" + unitPrice1;
            else if (unitPrice2 > 0)
                condition += " and unitPrice<=" + unitPrice2;

            int markUdeptId = _Request.GetInt("markUdept", 0);                                        if (markUdeptId > 0)
            {
                string deptIdList = CacheUtility.Cache_GetAllChildDept(markUdeptId.ToString());

                if (deptIdList == "")
                    condition += " and markUdeptid = " + markUdeptId;
                else
                    condition += " and markUdeptid in(" + deptIdList + ")";
            }

            string adduser = _Request.GetString("adduser", "");                                     if (adduser != "")
            {
                string userName = string.Empty;                 string userNo = string.Empty;                   int userId = 0;                                 _Utility.GetUserNoAndUserName(adduser.Trim(), out userName, out userNo);
                userId = ZLDC.Topo.BLL.Estate.UserBLL.GetUserIDByUserName(userName,userNo);

                condition += " and markuid = '" + userId + "'";
            }

            string fitment = _Request.GetString("fitment", "");                                     if (fitment != "" && fitment != "全部")
                condition += " and fitment = '" + fitment + "'";

            string houseStyle = _Request.GetString("houseStyle", "");                               if (houseStyle != "" && houseStyle != "全部")
                condition += " and houseStyle = '" + houseStyle + "'";

            string direct = _Request.GetString("direct", "");                                       if (direct != "" && direct != "全部")
                condition += " and direct = '" + direct + "'";

            string happliances = _Request.GetString("happliances", "");                             if (happliances != "")
                condition += " and happliances like '%" + happliances + "%'";

            string startdate = _Request.GetString("addDate1", "");                                  string enddate = _Request.GetString("addDate2", "");
            if (startdate != "" && enddate != "")
                                condition += " and DATEDIFF(dd, markdate, '" + startdate + "') <= 0 and DATEDIFF(dd, markdate, '" + enddate + "') >= 0";

            string regDate1 = _Request.GetString("regDate1", "");                                  string regDate2 = _Request.GetString("regDate2", "");

            if (regDate1 != "" && regDate2 != "")                                                      condition += " and DATEDIFF(dd, addDate, '" + regDate1 + "') <= 0 and DATEDIFF(dd, addDate, '" + regDate2 + "') >= 0 ";
           
            string type = _Request.GetString("type", "");                                           if (type != "" && type != "全部" && type != "无")
                condition += " and [housetype]='" + type + "'";

            int floor1 = _Request.GetInt("floor1", 0);                                              int floor2 = _Request.GetInt("floor2", 0);                                              if (floor1 > 0 && floor2 > 0)
                condition += " and " + floor1 + " <= mark1 and mark1 <= " + floor2;
            else if (floor1 > 0)
                condition += " and mark1 = " + floor1;
            else if (floor2 > 0)
                condition += " and mark1 = " + floor2;

            string roomno = _Request.GetString("roomno", "");                                       if (roomno != "")
                condition += " and roomno='" + roomno + "'";

            string InfoFrom = _Request.GetString("infoFrom", "");
            if (InfoFrom != "" && InfoFrom != "全部" && InfoFrom != "无")                           condition += " and source='" + InfoFrom + "'";

            return condition;
        }

                                        public static string GetCustomerCondition()
        {
            int deptId = _Convert.ToInt(_Utility.GetInfoBySession().m_sDeptID);
            string condition = "";

            string customerName = _Request.GetString("customerName", "");                   if (!string.IsNullOrEmpty(customerName))
                condition += " and customer like '%" + customerName + "%'";

            int room = _Request.GetInt("room", 0);                                          if (room > 0)
                condition += " and room=" + room;

            int hall = _Request.GetInt("hall", 0);                                          if (hall > 0)
                condition += " and hall=" + hall;

            int acreage1 = _Request.GetInt("acreage1", 0);                                  int acreage2 = _Request.GetInt("acreage2", 0);                                  if (acreage1 > 0 && acreage2 > 0)
                condition += " and minAcreage >=" + acreage1 + " and maxAcreage < " + acreage2;

            int status = _Request.GetInt("status", -1);                                     if (status > -1)
                condition += " and [status]=" + status;

            int isSelf = _Request.GetInt("isSelf", -1);                                     if (isSelf > -1)
                condition += " and [isSelf]=" + isSelf;

            int price1 = _Request.GetInt("price1", 0);                                      int price2 = _Request.GetInt("price2", 0);                                      if (price1 > 0 && price2 > 0)
                condition += " and minprice >=" + price1 + " and maxprice < " + price2;

            string tradeType = _Request.GetString("tradeType", "");                         if (tradeType != "" && tradeType != "全部")
                condition += " and tradeType = '" + tradeType + "'";

            return condition;
        }

                                        public static string GetCustomerConditionAdv()
        {
                       string condition = "";

            int typeid = _Request.GetInt("typeid", 1);                  if (typeid == 1)
                condition += "  and status<2";
            else if (typeid == 2)
                condition += " and status=2";

            string customerName = _Request.GetString("customerName", "");                   if (!string.IsNullOrEmpty(customerName))
                condition += " and customer='" + customerName + "'";

            int room1 = _Request.GetInt("room", 0);                                          int room2 = _Request.GetInt("hall", 0);
            if (room1 > 0 && room2 == 0)
            {
                condition += " and (room>='" + room1 + "' or room2>='" + room1 + "')";
            }
            else if (room2 > 0 && room1 == 0)
            {
                condition += " and (room1<='" + room2 + "' or room1>='" + room2 + "')";
            }
            else if (room1 >= 0 && room2 > 0)
            {
                                condition += " and (room between " + room1 + " and " + room2 + " or room1 between " + room1 + " and " + room2 + ")";
            }

            int acreage1 = _Request.GetInt("acreage1", 0);                                  int acreage2 = _Request.GetInt("acreage2", 0);                                  if (acreage1 > 0 && acreage2 > 0)
                condition += " and minAcreage >=" + acreage1 + " and maxAcreage < " + acreage2;

            string aname = _Request.GetString("aname", "");
            string dname = _Request.GetString("dname", "");
            if (dname != "" && dname != "全部")
                condition += " and charindex('" + dname + "', dname) > 0 ";

            if (aname != "" && aname != "全部")
                condition += " and charindex('" + aname + "', area) > 0 ";

            int price1 = _Request.GetInt("price1", 0);                                      int price2 = _Request.GetInt("price2", 0);                                      if (price1 > 0 && price2 > 0)
                condition += " and minprice >=" + price1 + " and maxprice < " + price2;

            string customerClass = _Request.GetString("customerClass", "");                 if (customerClass != "" && customerClass != "全部")
                condition += " and customerClass = '" + customerClass + "'";

            string tradeType = _Request.GetString("tradeType", "");                         if (tradeType != "" && tradeType != "全部")
                condition += " and tradeType = '" + tradeType + "'";

            string infoFrom = _Request.GetString("infoFrom", "");
            if (infoFrom != "" && infoFrom != "全部" && infoFrom != "无")                           condition += " and infofrom='" + infoFrom + "'";

                        string fitment = _Request.GetString("fitment", "");                             if (fitment != "" && fitment != "全部")
                condition += " and fitment=" + fitment;

            int floor1 = _Request.GetInt("floor1", 0);                                        int floor2 = _Request.GetInt("floor2", 0);                                        if (floor1 > 0 && floor2 > 0)
                condition += " and " + floor1 + " >= minfloor and " + floor2 + " <= maxFloor ";
            else if (floor1 > 0)
                condition += " and " + floor1 + " >= minfloor and " + floor1 + " <= maxFloor ";
            else if (floor2 > 0)
                condition += " and " + floor2 + " >= minfloor and " + floor2 + " <= maxFloor ";

            int lookCount1 = _Request.GetInt("lookCount1", 0);            int lookCount2 = _Request.GetInt("lookCount2", 0);
            if (lookCount1 > 0 && lookCount2 == 0)
            {
                condition += " and mark2='" + lookCount1 + "' ";
            }
            else if (lookCount2 > 0 && lookCount1 == 0)
            {
                condition += " and mark2='" + lookCount2 + "' ";
            }
            else if (lookCount1 > 0 && lookCount2 > 0) 
            {
                condition += " and mark2 between " + lookCount1 + " and " + lookCount2 + "";
            }


            string startdate = _Request.GetString("addDate1", "");                          string enddate = _Request.GetString("addDate2", "");
            if (startdate != "" && enddate != "")
                condition += " and DATEDIFF(dd, ownerDate, '" + startdate + "') <= 0 and DATEDIFF(dd, ownerDate, '" + enddate + "') >= 0";

            string regDate1 = _Request.GetString("regDate1", "");                          string regDate2 = _Request.GetString("regDate2", "");
            if (regDate1 != "" && regDate2 != "")
                condition += " and DATEDIFF(dd, AddDate, '" + regDate1 + "') <= 0 and DATEDIFF(dd, AddDate, '" + regDate2 + "') >= 0";



            int usage = _Request.GetInt("usage", 0);                                        if (usage > 0)
                condition += " and [usage]=" + usage;

            string houseStyle = _Request.GetString("houseStyle", "");                       if (houseStyle != "" && houseStyle != "全部" && houseStyle != "无")
                condition += " and [houseStyle]='" + houseStyle + "'";

            string direct = _Request.GetString("direct", "");                               if (direct != "" && direct != "全部" && direct != "无")
                condition += " and [direct]='" + direct + "'";

            string type = _Request.GetString("type", "");                                   if (type != "" && type != "全部" && type != "无")
                condition += " and [type]='" + type + "'";

            int status = _Request.GetInt("status", -1);                                     if (status > -1)
                condition += " and [status]=" + status;

            int isSelf = _Request.GetInt("isSelf", -1);                                     if (isSelf > -1)
                condition += " and [isSelf]=" + isSelf;

            int isChange = _Request.GetInt("isChange", -1);                                 if (isChange > 0)
                condition += " and [isChange] in(1,2)";

            string estates = _Request.GetString("estates", "");                             if (!string.IsNullOrEmpty(estates))
                condition += " and estates like'%" + estates + "%'";

            string remark = _Request.GetString("remark", "");
            if (remark != "")
            {
                condition += " and contains((remark),'\"" + remark + "\" or \"*" + remark + "*\"')";
            }


            int ischangeout = _Request.GetInt("ischangeout", 0);
            int ownerDeptId = _Request.GetInt("ownerDept", 0);                                        if (ownerDeptId > 0)
            {
                string deptIdList = CacheUtility.Cache_GetAllChildDept(ownerDeptId.ToString());

                if (ischangeout == 1)
                {
                    if (deptIdList == "")
                        condition += " and (ownerDeptId = " + ownerDeptId + " or (addDeptId = " + ownerDeptId + " and ischange=2)) ";
                    else
                        condition += " and (ownerDeptId in(" + deptIdList + ") or (addDeptId in(" + deptIdList + ") and ischange=2))";
                }
                else
                {
                    if (deptIdList == "")
                        condition += " and ownerDeptId = " + ownerDeptId;
                    else
                        condition += " and ownerDeptId in(" + deptIdList + ")";
                }
            }

            string owner = _Request.GetString("owner", "");                                 string userName = string.Empty;             string userNo = string.Empty;               _Utility.GetUserNoAndUserName(owner.Trim(), out userName, out userNo);
            ZLDC.Topo.Model.Estate.UserInfo item = ZLDC.Topo.BLL.Estate.UserBLL.GetUserInfoByUserNo(userNo);
            if (item != null && item.Id > 0)
            {


                if (ischangeout == 1)
                {
                    condition += " and (ownerid = '" + item.Id + "' or (ischange=2 and addUid='" + item.Id + "'))";
                }
                else
                {
                    condition += " and ownerid = '" + item.Id + "'";
                }
            }

            return condition;
        }

                                                public static string GetChineseUseByID(string useStr)
        {
            string useValue = "";
            if (useStr == "1")
                useValue = "住宅";
            else if (useStr == "2")
                useValue = "商铺";
            else if (useStr == "3")
                useValue = "写字楼";
            else if (useStr == "4")
                useValue = "商住";
            else if (useStr == "5")
                useValue = "仓库";
            else if (useStr == "6")
                useValue = "厂房";
            else if (useStr == "7")
                useValue = "地皮";
            else if (useStr == "8")
                useValue = "项目盘";
            else if (useStr == "9")
                useValue = "其他*";
            else if (useStr == "10")
                useValue = "民房";
            else if (useStr == "11")
                useValue = "车位";

            return useValue;
        }

                                                                                                                                                                                                                
                    }
}
